<?php

namespace App\Http\Controllers\Api;

use App\Services\CategoryService;
use App\Services\PageService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Model\Category;
use App\Model\Meta;
use App\Model\Page;
use App\Services\CommentService;
use App\Services\Util;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;

class PostController extends ApiController
{
    public function listPosts()
    {
        $orm = new Page();
        $p = Input::get('p',5);
        $orm = $orm->where('type',Page::TYPE_POST)->orderBy('created_at','desc');
        $navs = array();
        $date = Input::get('date');
        if(!empty($date)){
            array_push($navs,Util::array2Obj(array('key'=>'date','val'=>$date)));
            $st = Util::getMonth($date)[0];
            $et = Util::getMonth($date)[1];
            $orm = $orm->where('created_at','>',$st)->where('created_at','<',$et);
        }
        $keyword = Input::get('keyword');
        if(!empty($keyword)){
            $orm = $orm->where('title','like','%'.$keyword.'%');
        }
        $tag = Input::get('tag');
        if(!empty($tag)){
            $tag = Category::where('name',$tag)->where('type',Category::TYPE_TAG)->first();
            if(empty($tag)){
                return self::error('标签错误');
            }
            $ids  = array();
            $_tag = '%"' . $tag->id . '"%';
            $metas = Meta::where('tags','like',$_tag)->get(['page_id']);
            foreach ($metas as $meta){
                array_push($ids,$meta->page_id);
            }
            $orm = $orm->whereIn('id',$ids);
            array_push($navs,Util::array2Obj(array('key'=>'tag','val'=>$tag->name)));
        }
        $cate = Input::get('cate');
        if(!empty($cate)){
            $cate = Category::where('name',$cate)->where('type',Category::TYPE_CATE)->first();
            if(empty($cate)){
                return self::error('标签错误');
            }
            $ids  = array();
            $_cate = '%"' . $cate->id . '"%';
            $metas = Meta::where('categorys','like',$_cate)->get(['page_id']);
            foreach ($metas as $meta){
                array_push($ids,$meta->page_id);
            }
            $orm = $orm->whereIn('id',$ids);
            array_push($navs,Util::array2Obj(array('key'=>'cate','val'=>$cate->name)));
        }
        $posts = $orm->paginate($p);
        $posts->setPath(env('APP_URL').'/index.php/api/posts?date='.$date.'&keyword='.$keyword.'&tag='.$tag);

        foreach($posts as &$post){
            $post->month = Util::getDate('m',$post->created_at);
            $post->day   = Util::getDate('d',$post->created_at);
            $post->meta = $post->getMeta();
        }
        //跨域调用标识
        $callback = Input::get('callback');
        return self::apiSucc($posts,$callback);
    }

    public function article()
    {
        $id = Input::get('id');
        $callback = Input::get('callback');
        Log::debug($id);
        $post = Page::where('id',$id)->where('type',Page::TYPE_POST)->first();
        if(empty($id)||empty($post)){
            return self::apiFail('error',$callback);
        }
        $commentSrv = new CommentService();
        $post->comment = $commentSrv->getComments($post->id);
        $meta = Meta::where('page_id',$post->id)->first();
        $meta->views = (int)$meta->views + 1;
        $meta->save();
        $post->meta = $meta;
        return self::apiSucc($post,$callback);
    }

    public function searchPost()
    {
        $keyword = Input::get('keyword');
        $orm = new Page();
        $orm = $orm->where('type',Page::TYPE_POST)->orderBy('created_at','desc');
        if(!empty($keyword)){
            $orm = $orm->where('title','like','%'.$keyword.'%');
        }
        $posts = $orm->get(['id','title']);
        //跨域调用标识
        $callback = Input::get('callback');
        return self::apiSucc($posts,$callback);
    }

    public function getCates()
    {
        $dates = PageService::catePages();
        $cates = CategoryService::cates();
        //跨域调用标识
        $callback = Input::get('callback');
        return self::apiSucc([$dates,$cates],$callback);
    }
}
